/*
 * MapLayer.java
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster.gui.map;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.fpdev.core.Scenario;
import org.fpdev.core.basenet.BLink;
import org.fpdev.apps.rtemaster.RouteMaster;
import org.fpdev.apps.rtemaster.gui.RMGUI;

public class MapLayer {

  private RouteMaster av_;
  private RMGUI gui_;
  private int id_;
  private String name_;
  private Map<Short, Set<BLink>> visibleLinks_; // maps scenario id --> set of visible links

  public MapLayer(int id, String name, RouteMaster av, RMGUI gui) {
    av_ = av;
    gui_ = gui;
    id_ = id;
    name_ = name;
  }

  public void clearVisibleLinks(Iterator<Scenario> scenarios) {
    visibleLinks_ = new HashMap<Short, Set<BLink>>();
    while (scenarios.hasNext()) {
      Scenario s = scenarios.next();
      visibleLinks_.put(s.getID(), new HashSet<BLink>());
    }
  }

  public void classifyLink(BLink link) {
    visibleLinks_.get(link.getScenario().getID()).add(link);
  }

  /*public List<BLink> draw(MapCanvas canvas, boolean drawShpPts) {
    List<BLink> drawn = new LinkedList<BLink>();
    Iterator<Entry<Short, Set<BLink>>> entries = visibleLinks_.entrySet().iterator();
    //Iterator<Set<CLink>> sets = visibleLinks_.values().iterator();
    while (entries.hasNext()) {
      Entry<Short, Set<BLink>> entry = entries.next();
      if (!gui_.getScenariosPanel().isSelected(entry.getKey(), id_)) {
        continue;
      }
      Set<BLink> set = entry.getValue();
      Iterator<BLink> links = set.iterator();
      while (links.hasNext()) {
        BLink link = links.next();
        link.drawBG(canvas);
        drawn.add(link);
      }
      links = set.iterator();
      while (links.hasNext()) {
        BLink link = links.next();
        link.drawFG(canvas);
        if (drawShpPts) {
          link.drawShapePoints(canvas);
        }
      }
    }
    return drawn;
  }*/
  
  private List<BLink> visible_;

  public List<BLink> initVisible() { //MapCanvas canvas, boolean drawShpPts) {
    visible_ = new LinkedList<BLink>();
    Iterator<Entry<Short, Set<BLink>>> entries = visibleLinks_.entrySet().iterator();
    //Iterator<Set<CLink>> sets = visibleLinks_.values().iterator();
    while (entries.hasNext()) {
      Entry<Short, Set<BLink>> entry = entries.next();
      if (!gui_.getScenariosPanel().isSelected(entry.getKey(), id_)) {
        continue;
      }
      Set<BLink> set = entry.getValue();
      Iterator<BLink> links = set.iterator();
      while (links.hasNext()) {
        visible_.add(links.next());
      }
    }
    return visible_;
  }

  public void drawBG(MapCanvas canvas) {
    Iterator<BLink> links = visible_.iterator();
    while (links.hasNext()) {
      links.next().drawBG(canvas, av_.getActiveScenario());
    }
  //System.out.println("  drew "+visible_.size()+" links in bg");
  }

  public void drawFG(MapCanvas canvas, boolean drawShpPts) {
    Iterator<BLink> links = visible_.iterator();
    while (links.hasNext()) {
      BLink link = links.next();
      link.drawFG(canvas, av_.getActiveScenario());
      if (drawShpPts) {
        link.drawShapePoints(canvas);
      }
    }
  //System.out.println("  drew "+visible_.size()+" links in fg");
  }

  public String getName() {
    return name_;
  }

  public int getID() {
    return id_;
  }
}
